package io.bba.boot.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.bba.boot.framework.exception.BusinessException;
import io.bba.boot.framework.page.OrderByItem;
import io.bba.boot.framework.page.OrderMapping;
import io.bba.boot.framework.page.Paging;
import io.bba.boot.system.dto.SysBannerDto;
import io.bba.boot.system.entity.SysBanner;
import io.bba.boot.system.mapper.SysBannerMapper;
import io.bba.boot.system.query.SysBannerQuery;
import io.bba.boot.system.service.SysBannerService;
import io.bba.boot.system.vo.SysBannerVo;
import io.bba.boot.system.query.AppSysBannerQuery;
import io.bba.boot.system.vo.AppSysBannerVo;
import io.bba.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * 系统轮播图 服务实现类
 *
 * @author xxcj
 * @since 2024-03-13
 */
@Slf4j
@Service
public class SysBannerServiceImpl extends ServiceImpl<SysBannerMapper, SysBanner> implements SysBannerService {

    @Autowired
    private SysBannerMapper sysBannerMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addSysBanner(SysBannerDto dto) {
        SysBanner sysBanner = new SysBanner();
        BeanUtils.copyProperties(dto, sysBanner);
        return save(sysBanner);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateSysBanner(SysBannerDto dto) {
        Long id = dto.getId();
        SysBanner sysBanner = getById(id);
        if (sysBanner == null) {
            throw new BusinessException("系统轮播图不存在");
        }
        BeanUtils.copyProperties(dto, sysBanner);
        return updateById(sysBanner);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteSysBanner(Long id) {
        return removeById(id);
    }

    @Override
    public SysBannerVo getSysBannerById(Long id) {
        return sysBannerMapper.getSysBannerById(id);
    }

    @Override
    public Paging<SysBannerVo> getSysBannerPage(SysBannerQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<SysBannerVo> list = sysBannerMapper.getSysBannerPage(query);
        Paging<SysBannerVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppSysBannerVo getAppSysBannerById(Long id) {
        return sysBannerMapper.getAppSysBannerById(id);
    }

    @Override
    public Paging<AppSysBannerVo> getAppSysBannerPage(AppSysBannerQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppSysBannerVo> list = sysBannerMapper.getAppSysBannerPage(query);
        Paging<AppSysBannerVo> paging = new Paging<>(list);
        return paging;
    }

}
